import path from 'path'
import koa, { Context } from 'koa'
import koaStatic from 'koa-static'  //导入静态
import koaBody from 'koa-body'
import koaRouter from 'koa-router'  //导入路由
import favicon from 'koa-favicon'

import log from './common/logger'
import addRouter from './router'
//import tpl from './middleware/tpl'
import errorHandler from './middleware/error'

import views from 'koa-views'

const app = new koa()
const router = new koaRouter();
const baseDir = path.normalize(__dirname + '/..')

// session

// parse request
app.use(koaBody({
  jsonLimit: 1024 * 1024 * 5,
  formLimit: 1024 * 1024 * 5,
  textLimit: 1024 * 1024 * 5,
  multipart: true,// 解析FormData数据
  formidable: { uploadDir: path.join(baseDir, 'upload') }  // 设置文件上传目录
}));

// set static directory
app.use(koaStatic(path.join(baseDir, 'public'), { index: false }));  //设置静态文件目录
app.use(favicon(path.join(baseDir, 'public/favicon.jpg')));

/*
//set template engine 
app.use(tpl({ path: path.join(baseDir, 'views') }));
*/

//配置视图
app.use(views(path.join(baseDir, './views'), {
	extension: 'ejs'
  }))

//git

// handle the error
app.use(errorHandler());

// add route
addRouter(router);
app.use(router.routes()).use(router.allowedMethods());

// deal 404
app.use(async (ctx: Context) => {
  log.error(`404 ${ctx.message} : ${ctx.href}`);
  ctx.status = 404;
  ctx.body = '404! content not found !';
});

// koa already had middleware to deal with the error, just register the error event
app.on('error', (err, ctx: Context) => {
  log.error(err);//log all errors
  ctx.status = 500;
  if (ctx.app.env !== 'development') { //throw the error to frontEnd when in the develop mode
    ctx.res.end(err.stack); //finish the response
  }
  // else {
  //   ctx.body = 'Server Error';
  // }
});

if (!module.parent) {
  const port = process.env.PORT || 8000;
  app.listen(port);
  // http.createServer(app.callback()).listen(port);// does the same like: app.listen(port)
  log.info(`=== app server running on port ${port}===`);
  console.log('app server running at: http://localhost:%d/index', port);
}